<template>
  <BaseDialog
    ref="baseDialog"
    width="55%"
    title="通道控制"
    @confrim="confrim"
    :hideFooter="true"
  >
    <h3>IMEI:{{ imei }}</h3>
    <BaseForm
      labelWidth="105px"
      :fromIems="fromIems"
      :size="'medium'"
      ref="BaseForm"
      :inline="true"
      @submit="submit"
      @edit="edit"
    >
    </BaseForm>
    <div class="footer">
      <el-button type="primary" @click="handleSubmit">发送通道命令</el-button>
    </div>
  </BaseDialog>
</template>

<script>
import { sendChannelCmd } from "@/api/device";
import { Message } from "element-ui";
export default {
  data() {
    return {
      imei: "",
      fromIems: [],
    };
  },
  methods: {
    open(data) {
      console.log(data.channelData);
      console.log(data.channelDes);
      this.fromIems = [];
      if (!data.channelData || !data.channelDes) {
        Message.error("没有通道数据");
        return;
      }
      let desc = JSON.parse(data.channelDes);
      let labels = [];
      let dataIndexList = [];
      for (var k in desc) {
        labels.push(desc[k]);
        dataIndexList.push(k);
      }
      console.log(labels, "xx");
      let fromList = JSON.parse(data.channelData);
      fromList.forEach((el, index) => {
        this.fromIems.push({
          valueType: "input",
          label: labels[index],
          dataIndex: dataIndexList[index],
          defaultValue: el ? el : "0",
        });
      });
      this.imei = data.imei;
      this.$refs.baseDialog.open();
    },
    confrim() {
      this.close();
    },
    close() {
      this.$refs.baseDialog.close();
    },
    async submit(data) {
      let arr = [];
      for (var k in data) {
        arr.push(Number(data[k]));
      }
      console.log(arr, "xxx");
      const res = await sendChannelCmd({
        imei: this.imei,
        data: arr,
      });
      if (!res.status) {
        Message.success("发送成功");
        this.close();
      }
    },
    edit(data) {},
    handleSubmit() {
      this.$refs.BaseForm.submit();
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  display: flex;
  overflow: hidden;
}
.footer {
  text-align: right;
}
</style>
